package com.yundingai.ai.tools;

import com.yundingai.domain.dto.PlanDTO;
import com.yundingai.domain.entity.AllPlan;
import com.yundingai.domain.entity.StudyInfo;
import com.yundingai.service.AllPlanService;
import com.yundingai.service.StudyInfoService;
import io.lettuce.core.dynamic.annotation.Param;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: liyuxiang
 * @Date: 2025/7/17 13:42
 */

@Component
@Slf4j
public class PlanFunction {
    @Autowired
    AllPlanService allPlanService;
    @Autowired
    StudyInfoService studyInfoService;

    /**
     * 查看用户学习计划，用户id
     *
     * @return
     */
    @Tool(description = "查询我的学习计划")
    public List<AllPlan> getPlanList(@Param(value = "userId") String userId) {
        log.info("查询我的学习计划=>");
        return allPlanService.selectPlansByUserId(Integer.parseInt(userId));
    }

    @Tool(description = "分析我的学习状态")
    public List<AllPlan> analysis() {
        log.info("查询我的学习计划=>");
        return allPlanService.list();
    }

    @Tool(description = "按照计划内容出练习题")
    public List<AllPlan> setQuestions(@Param(value = "userId") String userId) {
        log.info("查询我的学习计划=>");
        return allPlanService.selectPlansByUserId(Integer.parseInt(userId));
    }

    /**
     * TODO 添加用户的学习信息，学历、学习科目和学习目标等信息，ReceiveSubjectListVo
     */

    @Tool(description = "当用户没有输入具体科目名称时按照数据库中用户的学历、学习科目和学习目标添加一个新的学习计划")
    public String addPlanByUserStudentInfo(@Param(value = "计划内容") PlanDTO studyPlan, @Param(value = "userId") String userId) {
        //TODO 查询用户是否填写学历、学习科目、学习目标等信息，如果没有则提示用户先填写这些信息
        StudyInfo studyInfo = studyInfoService.selectStudyInfoVoByUserId(Integer.parseInt(userId));
        if (studyInfo == null || studyInfo.getEducation() == null || studyInfo.getEducation().isEmpty()) {
            return "错误：请先填写您的学历、学习科目和学习目标等信息";
        }
        try {
            if (studyPlan.getPlanName() == null || studyPlan.getPlanName().isEmpty()) {
                return "错误：学习计划必须包含总标题";
            }
            log.info("添加学习计划: {}", studyPlan);
            AllPlan allPlan = new AllPlan();
            allPlan.setPlanName(studyPlan.getPlanName());
            allPlan.setPlanContent(studyPlan);
            allPlanService.addPlan(allPlan, Integer.parseInt(userId));
            return "添加成功，计划标题: " + studyPlan.getPlanName();
        } catch (Exception e) {
            log.error("添加学习计划失败: {}", e.getMessage());
            return "错误：添加学习计划失败，请稍后再试";
        }
    }

    @Tool(description = "按照用户输入的计划标题，添加一个新的学习计划")
    public String addPlan(@Param(value = "计划内容") PlanDTO studyPlan, @Param(value = "userId") String userId) {
        try {
            if (studyPlan.getPlanName() == null || studyPlan.getPlanName().isEmpty()) {
                return "错误：学习计划必须包含总标题";
            }
            log.info("添加学习计划: {}", studyPlan);
            AllPlan allPlan = new AllPlan();
            allPlan.setPlanName(studyPlan.getPlanName());
            allPlan.setPlanContent(studyPlan);
            allPlanService.addPlan(allPlan, Integer.parseInt(userId));
            return "添加成功，计划标题: " + studyPlan.getPlanName();
        } catch (Exception e) {
            log.error("添加学习计划失败: {}", e.getMessage());
            return "错误：添加学习计划失败，请稍后再试";
        }
    }

    @Tool(description = "按照用户输入的计划标题，删除一个学习计划")
    public String deletePlan(@Param(value = "计划标题") String planName, @Param(value = "userId") String userId) {
        if (planName == null || planName.isEmpty()) {
            return "错误：学习计划标题不能为空";
        }
        log.info("删除学习计划: {}", planName);
        int delResult = allPlanService.deleteByPlanName(planName, userId);
        if (delResult > 0) {
            return "删除成功，计划标题: " + planName;
        } else {
            return "删除失败，可能计划不存在";
        }
    }

}
